import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { BladeData } from "../../../../../shared/blade/blade-data";
import { Blade } from "../../../../../shared/blade/blade.model";
import { Command } from "../../../../../shared/toolbar.service";
import { WindPowers } from "../../../../../entity/wind-powers.model";
import { WindPowersService } from "../../../../../service/wind-powers.service"
@Component({
  selector: 'app-standard-curve-edit',
  templateUrl: './standard-curve-edit.component.html',
  styleUrls: ['./standard-curve-edit.component.css']
})
export class StandardCurveEditComponent extends BladeContainerBase<WindPowers> {
  @Output() clearText = new EventEmitter<boolean>();

  constructor(
    private fb: FormBuilder,
    private windPowersService: WindPowersService
  ) {
    super(fb)
  }

  ngOnInit() {
    if (!this.isNew) {
      this.commands = [
        new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
        new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
      ]

    }
    super.ngOnInit();
  }
  initData(): any | Observable<any> | Promise<any> {

    this.formPatchValue(this.currentEntity);

  }
  exeSaveChange(blade: any): void {
    this.saveChange();
  }
  canSaveChange(blade: any): boolean {
    return this.hasChange() && !this.hasError();
  }
  exeReset(blade: any): void {
    return this.formGroup.reset(this.currentEntity);
  }
  canReset(blade: any): boolean {
    return this.hasChange();
  }
  createForm() {
    let obj: {} = {
      "wind": [this.currentEntity.wind,
      [
        Validators.required,
        Validators.pattern("^[0-9]+([.]{1}[0-9]+){0,1}$"),
        Validators.max(25),
        Validators.maxLength(5),
      ]
      ],
      "power": [this.currentEntity.power,
      [
        Validators.required,
        Validators.pattern("^[0-9]+([.]{1}[0-9]+){0,1}$"),
        Validators.max(10000),
        Validators.maxLength(7),
      ]]
    };
    this.formGroup = this.fromBuider.group(obj);
  }
  /**
  * 设置表单错误对象
  * 
  * 
  * @memberof BladeContainerBase
  */
  setFormError(): void {
    this.formErrors = {
      "wind": "",
      "power": ""
    };
  }
  /**
  * 设置表单验证信息
  * 
  * 
  * @memberof BladeContainerBase
  */
  setValidationMessages(): void {
    this.validationMessages = {

      "wind": {
        'required': '风速不能为空。',
        'pattern': '风速必须由数字组成。',
        'maxlength': '风速不能多于5个字符长度。',
        'max':'风速最大不能超过25'
      },
      "power": {
        'required': '风速不能为空。',
        'pattern': '风速必须由数字组成。',
        'maxlength': '风速不能多于7个字符长度。',
        'max':'风速最大不能超过10000'
      },
    };
  }

  /**
   * 创建一个测点信息
   * 
   * 
   * @memberof TurbstatusEditComponent
   */
  onCreate() {
    if (!this.hasError()) {
      this.changeCurrent();
      this.windPowersService.create(this.currentEntity)
        .then(res => {
          if (res) {
            let parentContainerInstance = this.parentBlade.bladeContainerInstance;
            parentContainerInstance.refresh(res);
            this.bladeService.closeBlade(this.currentBlade);
          } else {
            throw new Error("数据新增失败！")
          }
        })
        .catch(err => {
          console.log(err)
        })
        .then(res => this.bladeData.isLoading = false);
      
    }
  }
  /**
   * 
   * 
   * 
   * @memberof TagsEditComponent
   */
  saveChange(): any | Promise<any> | Observable<any> {

    if (this.canSaveChange(this.currentBlade)) {
      this.changeCurrent()
        return this.windPowersService.update(this.currentEntity)
         .then(res => {
           if (res) {
             this.clearText.emit(true);
             (this.parentBlade.bladeContainerInstance).refresh(res);
             this.bladeService.closeBlade(this.currentBlade);
             return true;
           }
          throw new Error("数据更新失败！")
 
         })
         .catch(error => this.bladeData.error = error)
         .then(res => this.bladeData.isLoading = false); 
    }
  }
  changeCurrent() {
    this.bladeData.isLoading = true;
    let formValue = this.formGroup.value;
    this.currentEntity.wind=formValue.wind;
    this.currentEntity.power=formValue.power;

  }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/model/standard-curve/standard-curve-edit/standard-curve-edit.component.ts